package com.silence.springboot.convert;

import com.silence.springboot.entity.UserBaseInfoEntity;
import com.silence.springboot.enums.SexEnum;
import com.silence.springboot.vo.input.AdminAddUserBaseInfoInput;
import com.silence.springboot.vo.input.AdminUpdateUserBaseInfoInput;
import com.silence.springboot.vo.input.SearchByConditionInput;
import com.silence.springboot.vo.output.PageOutput;
import com.silence.springboot.vo.output.QueryUserBaseInfoByTokenOutput;
import com.silence.springboot.vo.output.SearchByConditionOutput;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.springframework.stereotype.Component;

/**
 * 用户基础信息表 转换器接口。
 */
@Component
@Mapper(componentModel = "spring")
public interface UserBaseInfoConvert {

    /**
     * 将用户基础信息表实体类转换为根据token查询用户信息的输出视图
     *
     * @param entity 用户基础信息表实体类
     * @return 根据token查询用户信息的输出视图
     */
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "sex", target = "sex", qualifiedByName = "sexEnumToString")
    QueryUserBaseInfoByTokenOutput toQueryUserBaseInfoByTokenOutput(UserBaseInfoEntity entity);

    /**
     * 将用户基础信息表实体类转换为分页查询用户基础信息的输出视图
     *
     * @param entity 用户基础信息表实体类
     * @return 分页查询用户基础信息的输出视图
     */
    @Mapping(source = "sex", target = "sex", qualifiedByName = "sexEnumToString")
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "updateTime", target = "updateTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    PageOutput toPageOutput(UserBaseInfoEntity entity);

    /**
     * 新增用户基础信息入参转换
     *
     * @param input 新增用户基础信息入参
     * @return 用户基础信息表实体类
     */
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    UserBaseInfoEntity adminAddUserBaseInfoInputToUserBaseInfoEntity(AdminAddUserBaseInfoInput input);

    /**
     * 修改用户基础信息入参转换
     *
     * @param input 修改用户基础信息入参
     * @return 用户基础信息表实体类
     */
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    UserBaseInfoEntity adminUpdateUserBaseInfoInputToUserBaseInfoEntity(AdminUpdateUserBaseInfoInput input);

    /**
     * 条件查询用户基础信息入参转换
     *
     * @param input 条件查询用户基础信息入参
     * @return 用户基础信息表实体类
     */
    UserBaseInfoEntity searchByConditionInputToUserBaseInfoEntity(SearchByConditionInput input);

    /**
     * 条件查询用户基础信息出参转换
     *
     * @param entity 用户基础信息表实体类
     * @return 条件查询用户基础信息出参
     */
    @Mapping(source = "sex", target = "sex", qualifiedByName = "sexEnumToString")
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "updateTime", target = "updateTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    SearchByConditionOutput toSearchByConditionOutput(UserBaseInfoEntity entity);

    /**
     * 性别枚举转换为字符串
     *
     * @param sex 性别枚举
     * @return 性别字符串
     */
    @Named(value = "sexEnumToString")
    default String sexEnumToString(String sex) {
        try {
            int sexCode = Integer.parseInt(sex);
            return SexEnum.getDescByCode(sexCode);
        } catch (Exception e) {
            return "";
        }
    }
}
